//
//  TabBar.m
//  BaiduTravel
//
//  Created by guantianpeng on 13-5-10.
//  Copyright (c) 2013年 Baidu.com. All rights reserved.
//

#import "TabBar.h"
#import "ImageDefs.h"

@interface TabBar()

@property(nonatomic,retain) NSMutableArray *tabItems;
@property(nonatomic,retain) UIImage *imgItemLine;

- (void)initItemView:(NSArray *)titles;

- (BOOL)checkLeftOrRight:(int)index;

@end

@implementation TabBar
@synthesize tabItems = _tabItems;
@synthesize imgItemLine = _imgItemLine;
@synthesize tabBarDelegate = _tabBarDelegate;
@synthesize selectedIndex = _selectedIndex;

- (id)initWithFrame:(CGRect)frame tabBarTitles:(NSArray *)titles
{
    self = [super initWithFrame:frame];
    if (self) {
        _imgItemLine = [kImgTabItemLine retain];
        self.selectedIndex  = -1;
        
        [self initItemView:titles];
    }
    return self;
}

- (void)initItemView:(NSArray *)titles
{
    int count = [titles count];
    _tabItems = [[NSMutableArray alloc]initWithCapacity:count];
    CGFloat w = self.frame.size.width / count;
    CGFloat h = self.frame.size.height;
    int i = 0;
    for (NSString *title in titles) {
        CGRect frame = CGRectMake(w * i, 0, w, h);
        TabItem *item = [[TabItem alloc]initWithFrame:frame];
        
        item.tabItemDelegate = self;
        item.tag = i;
        item.title = title;
        [self addSubview:item];
        [_tabItems addObject:item];
        [item release];
        //for lines
        if (i > 0) {
            UIImageView *line = [[UIImageView alloc]initWithFrame:CGRectMake(w * i - _imgItemLine.size.width / 2.0, (h - _imgItemLine.size.height) / 2.0, _imgItemLine.size.width, _imgItemLine.size.height)];
            [line setImage:_imgItemLine];
            [self addSubview:line];
            [line release];
        }
        i++;
    }
    
}

#pragma mark - handle the item selected event
- (void)didSelectTabItem:(TabItem *)itemView
{
    // find the previos selected item
    TabItem *prevItem = nil;
    for (TabItem *item in _tabItems) {
        if (item.itemSelected) {
            prevItem = item;
            break;
        }
    }
    self.selectedIndex = itemView.tag;
    
    // click the same item and return directly.
    if (prevItem == itemView) {
        itemView.itemSelected = NO;
        [itemView setItemSelectedBackground];
        if ([self.tabBarDelegate respondsToSelector:@selector(didUnselectTabBar:)]) {
            [self.tabBarDelegate didUnselectTabBar:prevItem.tag];
        }
        [self performSelector:@selector(resetItemsNormalBackground) withObject:nil afterDelay:0.5];
        return;
    }
    
    // set previous unselected state
    /*if (prevItem) {
        prevItem.itemSelected = NO;
        if ([self.tabBarDelegate respondsToSelector:@selector(didUnselectTabBar:)]) {
            [self.tabBarDelegate didUnselectTabBar:prevItem.tag];
        }
    }*/
    // set current selected state
    itemView.itemSelected = YES;
    if ([self.tabBarDelegate respondsToSelector:@selector(didSelectTabBar:)]) {
        [self.tabBarDelegate didSelectTabBar:itemView.tag];
    }
    
    [self performSelector:@selector(resetItemsNormalBackground) withObject:nil afterDelay:0.5];
}

// YES left, otherwise NO
- (BOOL)checkLeftOrRight:(int)index
{
    if (index % 2) {
        return NO;
    }
    else {
        return YES;
    }
}

- (void)clearState
{
    for (TabItem *item in _tabItems) {
        if (item.itemSelected) {
            item.itemSelected = NO;
        }
    }
}


- (void) setTitleName:(NSString *)titleName forTabIndex:(NSInteger)index
{
    TabItem *item = (TabItem *)[_tabItems objectAtIndex:index];
    item.title = titleName;
}
-(void) resetItemsNormalBackground
{
    [self performSelectorOnMainThread:@selector(resetItemsNormalBackgroundOnMainThread) withObject:nil waitUntilDone:NO];
}


-(void) resetItemsNormalBackgroundOnMainThread
{
    for (TabItem *item in _tabItems) {
        [item resetItemNormalBackground];
    }
}


-(void) setSelectedIndex:(NSInteger)selectedIndex
{
    for (TabItem *item in _tabItems) {
        item.itemSelected = NO;
        if (item.tag == selectedIndex) {
            item.itemSelected = YES;
        }
    }
    _selectedIndex = selectedIndex;
}

- (void)dealloc
{
    self.imgItemLine = nil;
    self.tabBarDelegate = nil;
    self.tabItems = nil;
   
    [super dealloc];
}

@end
